/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shop.web.controllers;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.shop.domain.Store;
import org.shop.service.InventoryService;
import org.shop.utils.InstanceUtils;
import org.shop.web.utils.RootController;

/**
 *
 * @author Vinnu
 */
public class SearchController extends RootController {

    private final static Logger log = Logger.getLogger(SearchController.class);

    @Override
    protected void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.debug("Search Controller Called....");
        InstanceUtils utils = new InstanceUtils();

        InventoryService service = utils.getService();
        PrintWriter out = response.getWriter();
        String param = request.getParameter("action");
        log.info(param);

        if (param.equalsIgnoreCase("itemSearch")) {
            Gson gson = new Gson();
            String itemId = request.getParameter("searchItemId");
            String itemName = request.getParameter("searchItemName");
            log.info(itemId + itemName);
            Store store = service.findByItem(itemId, itemName);
            log.info(gson.toJson(store));
            out.print(gson.toJson(store));
        }
    }
}
